package com.hlf.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atguigu.commonutils.R;
import com.hlf.eduservice.entity.CourseQuery;
import com.hlf.eduservice.entity.EduCourse;
import com.hlf.eduservice.entity.vo.CourseInfoVo;
import com.hlf.eduservice.entity.vo.CoursePublishVo;
import com.hlf.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hlf
 * @since 2022-11-27
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

@Autowired
    private EduCourseService courseService;

  @PostMapping("addCourseInfo")
  public  R  addCourseInfo(
          @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
          @RequestBody CourseInfoVo courseInfoVo){
      String courseId =  courseService.saveCourseInfo(courseInfoVo);//
      /*if(!StringUtils.isEmpty(courseId)){
          return R.ok().data("courseId", courseId);
      }else{
          return R.error().message("保存失败");
      }*/
      return R.ok().data("courseId",courseId);
  }

  // 根据课程id插询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
       CourseInfoVo courseInfoVo =   courseService.getCourseInfo(courseId);
       return R.ok().data("courseInfoVo",courseInfoVo);
    }

    // 修改课程信息
    @PostMapping("updateCourseInfo")
    public  R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
         courseService.updateCourseInfo(courseInfoVo);
         return R.ok();
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        CoursePublishVo courseInfoForm = courseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse", courseInfoForm);
    }

    @ApiOperation(value = "根据id发布课程")
    @PostMapping("publishCourse/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    @ApiOperation(value = "分页课程列表")
    @PostMapping("pageCourseCondition/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    CourseQuery courseQuery){
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

}

